<?php
namespace Home\Controller;
use Common\Controller\CommonController;

class WxContentController extends CommonController {
	
	function index(){
		$this->display('index');
	}
	//列表页面
	public function content_list(){
		$content=M('content');
		$list=$content->select();//print_r($list);exit;
		$this->assign('list',$list);
		$this->display('index');
	
	}
	//添加显示页面；响应菜单
	public function add_contentindex(){
		$menu=M('menu');
		$list=$menu->where('two_id=0')->select();//print_r($list);exit;
		foreach ($list as $k=>$v){
			$two_id[] = $v['id'];
		}
		if ($two_id) {
			$w['two_id'] = array('in',$two_id);
			$twomenu = M('menu')->where($w)->order('id desc')->select();//所有二级菜单；二维数组
		}
		foreach ($twomenu as $k=>$v){
			$newcate[$v['two_id']][] = $v;//print_r($v);exit;//所有一级菜单。一维数组
		}
		foreach ($list as $k=>$v){
			$list[$k]['list'] = $newcate[$v['id']];
		}
		//print_r($list);exit;
		$this->assign('list',$list);
		$this->display('add_content');
	}
	//添加内容
	public function add_content(){
		//接收参数
		$content=M('content');
		$con=trim(I('content'));
		$explain=trim(I('explain'));
		$type=trim(I('type'));
		$cover=trim(I('cover'));
		$voice=trim(I('voice'));
		$editor=I('editor');
		$ctime=date('Y-m-d H:i:s',time());
		$mid=trim(I('mid'));
		//查找对应的key
		$key=M('menu')->where('id='.$mid)->find();
		if ($key['one_key']!='') {
			$data=array(
				'explain'=>$explain,
				'type'=>$type,
				'mid'=>$mid,
				'content'=>$con,
				'c_menu'=>$key['one_key'],
				'cover'=>$cover,
				'editor'=>$editor,
				'voice'=>$voice,
				'ctime'=>$ctime,
				'mtime'=>$ctime,
				);
		//print_r($data);exit('1');
		$res_add=$content->data($data)->add();
		//echo M()->_sql();exit('1');
		}else{
			$data=array(
				'explain'=>$explain,
				'type'=>$type,
				'mid'=>$mid,
				'content'=>$con,
				'c_menu'=>$key['two_key'],
				'cover'=>$cover,
				'editor'=>$editor,
				'voice'=>$voice,
				'ctime'=>$ctime,
				'mtime'=>$ctime,
				);
		//print_r($data);exit('2');
		$res_add=$content->data($data)->add();
		//echo M()->_sql();exit('1');
		}
		if (false !== $res_add) {
			 $this->redirect('WxContent/content_list');
		}else{
			 $this->redirect('WxContent/content_list');
		}
		
	}
	//删除内容
	public function del_content(){
		$id=I('id');
		$content=M('content');
		$list=$content->where('id='.$id)->find();

		$res_del=$content->where('id='.$id)->delete();
		//echo M()->_sql();exit;
		if (false !== $res_del) {
		    $this->redirect('WxContent/content_list');
		}else{
		    $this->redirect('WxContent/content_list');
		}
	}



	//修改内容显示
	public function update_index(){
		$menu=M('menu');
		$list_menu=$menu->where('two_id=0')->select();//print_r($list);exit;
		foreach ($list_menu as $k=>$v){
			$two_id[] = $v['id'];
		}
		if ($two_id) {
			$w['two_id'] = array('in',$two_id);
			$twomenu = M('menu')->where($w)->order('id desc')->select();//所有二级菜单；二维数组
		}
		foreach ($twomenu as $k=>$v){
			$newcate[$v['two_id']][] = $v;//print_r($v);exit;//所有一级菜单。一维数组
		}
		foreach ($list_menu as $k=>$v){
			$list_menu[$k]['list'] = $newcate[$v['id']];
		}

		$id=I('id');
		$content=M('content');
		$list=$content->where('id="'.$id.'"')->find();// 当前内容
		//print_r($list);exit;
		$menu_id=$list['mid'];
		$menu_list=$menu->where('id="'.$menu_id.'"')->find();
		//print_r($menu_list);exit('111');
		$this->assign('list',$list_menu);//映射的是菜单列表
		
		$this->assign('data',$list);//映射的是content
		
		$this->assign('arr',$menu_list);//映射的是当前内容的菜单
		
		$this->display('update_content');		
	}
	//修改内容
	public function update_content(){
		$id=trim(I('id'));
		$content=M('content');
		$con=trim(I('content'));
		$explain=trim(I('explain'));
		$type=trim(I('type'));
		$cover=trim(I('cover'));
		$editor=I('editor');
		$voice=trim(I('voice'));
		$mid=trim(I('mid'));
		$ctime=date('Y-m-d H:i:s',time());
		//查找对应的key
		$key=M('menu')->where('id='.$mid)->find();
		if ($key['one_key']!='') {
			$data=array(
					'explain'=>$explain,
					'type'=>$type,
					'content'=>$con,
					'editor'=>$editor,
					'mid'=>$mid,
					'c_menu'=>$key['one_key'],
					'cover'=>$cover,
					'voice'=>$voice,
					'mtime'=>$ctime,
					);
			//print_r($data);exit;
			$res_save=$content->where('id='.$id)->data($data)->save();
			//echo M()->_sql();exit('111');
		}else{
			$data=array(
					'explain'=>$explain,
					'type'=>$type,
					'content'=>$con,
					'mid'=>$mid,
					'c_menu'=>$key['two_key'],
					'cover'=>$cover,
					'editor'=>$editor,
					'voice'=>$voice,
					'mtime'=>$ctime,
					);
			//print_r($data);exit;
			$res_save=$content->where('id='.$id)->data($data)->save();
			//echo M()->_sql();exit('222');
		}
		if (false !== $res_save) {
			//exit('111');
			 $this->redirect('WxContent/content_list');
		}else{
			//exit('222');
			 $this->redirect('WxContent/content_list');
		}
		
	}
	//关键字应答首页
	public function keyword(){
		$keyword=M('keyword');
		$list=$keyword->where('id>1')->select();
		$arr=$keyword->where('id=1')->find();
		$this->assign('arr',$arr);
		$this->assign('list',$list);
		$this->display('keyword');
		//echo 'hello';
	}
	//添加关键字首页
	public function add_keyword(){
		$this->display('keyword_add');
	}
	//添加关键字到数据库
	public function keyword_add(){
		$key=trim(I('keyword'));
		$content=trim(I('content'));
		$data=array(
			'keyword'=>$key,
			'content'=>$content,
			'ctime'=>date("Y-m-d H:i:s",time()),
			'mtime'=>date("Y-m-d H:i:s",time()),
			);
		//print_r($data);exit;
		$res_add=M('keyword')->data($data)->add();
		if (false == $res_add){
			exit('失败');
			$this->redirect('WxContent/keyword');
		}else{
			$this->redirect('WxContent/keyword');

		}
	}
	//删除关键字
	public function keyword_del(){
		$id=I('id');
		$res_del=M('keyword')->where('id="'.$id.'"')->delete();
		//echo M()->_sql();exit('@@@');
		if (false !== $res_del) {
		    $this->redirect('WxContent/keyword');
		}else{
			exit('fail');
		    $this->redirect('WxContent/keyword');
		}
	}
	//修改关键字回复页面
	public function keyword_update_index(){
		$id=I('id');
		$list=M('keyword')->where('id="'.$id.'"')->find();
		$this->assign('list',$list);
		$this->display('keyword_update');

	}
	//修改关键字回复
	public function keyword_update(){
		$id=I('id');
		$keyword=trim(I('keyword'));
		$content=trim(I('content'));
		$mtime=date("Y-m-d H:i:s",time());
		$data=array(
				'keyword'=>$keyword,
				'content'=>$content,
				'mtime'=>$mtime,
			);
		$res_save=M('keyword')->where('id="'.$id.'"')->save($data);
		if (false !== $res_save) {
			$this->redirect('WxContent/keyword');
		}else{
			$this->redirect('WxContent/keyword');
		}
	}	
	//文件上传
	function upload(){
		$file = $_FILES['Filedata'];
		$name=$file['name'];
		$a=substr(strrchr($name,'.'), 1);
		$name=time().'_'.rand(0,10000);
		$file['name']=$name.'.'.$a;
		$filename = './Public/upload/image/'.$file['name'];
		$rs = move_uploaded_file($file['tmp_name'],$filename);
		if($rs){
			$this->ajaxReturn(array('s'=>true,'m'=>'Success','d'=>$name,'c'=>$a));
		}else{
			$this->ajaxReturn(array('s'=>false,'m'=>'文件上传失败'));
		}
		
	}

	//删除图片
	function delimg(){
		$id=I('data');
		$filename = './Public/upload/image/'.$id;
		$re=unlink($filename);
		if($re){
			$this->ajaxReturn(array('s'=>true,id=>$id));
		}else{
			$this->ajaxReturn(array('s'=>false));
		}
	}


}